var $app = new Vue({
    el : '#app',
    data : {
        context : $contextPath||'',
        id : $id,
        loading : false,
        result : null,
        condition : {
            key : ''
        }
    },
    created : function(){
        var _this = this;
        _this.loading = true;
        setTimeout(function () {
            _this.query(function () {
                _this.initChart(function () {
                    _this.loading = false;
                });
            });
        },1200);
    },
    methods : {
        query : function(callback){
            var _this = this;
            $.ajax({
                url : _this.context + '/subject/getStatById',
                method : 'POST',
                data : {
                    id : _this.id,
                    result : {}
                },
                success : function(res){
                    console.log(res);
                    if(res){
                        _this.result = res.data;
                    }
                    //callback
                    callback && typeof callback == 'function' && callback();
                },
                error : function(){
                    _this.$message.warning('请求获取数据失败，请检查网络或登陆状态');
                }
            })
        },
        initChart : function(callback){
            var _this = this;
            var option = {
                title: {
                    text: '科目成绩分析',
                    subtext: '来自单科目成绩分析'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    // data: ['图标分析']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                yAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01]
                },
                xAxis: {
                    type: 'category',
                    data: ['最低分','最高分','平均分']
                },
                series: [
                    {
                        name: '分数',
                        type: 'bar',
                        data: [
                            _this.result.min,
                            _this.result.max,
                            _this.result.avg
                        ]
                    }
                ]
            };

            var chart = echarts.init(document.getElementById('chart'));
            chart.setOption(option);


            var option2 = {
                title: {
                    text: '科目成绩分析',
                    subtext: '来自单科目成绩分析'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    // data: ['图标分析']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01]
                },
                yAxis: {
                    type: 'category',
                    data: ['超均率','及格率','优良率']
                },
                series: [
                    {
                        name: '比例',
                        type: 'bar',
                        data: [
                            _this.result.rate4over,
                            _this.result.rate4d,
                            _this.result.rate4b
                        ]
                    }
                ]
            };

            var chart2 = echarts.init(document.getElementById('chart2'));
            chart2.setOption(option2);


            var option3 = {
                tooltip : {
                    formatter: "{a} <br/>{b} : {c}%"
                },
                toolbox: {
                    feature: {
                        restore: {},
                        saveAsImage: {}
                    }
                },
                series: [
                    {
                        name: '标准差',
                        type: 'gauge',
                        detail: {formatter:'{value}%'},
                        data: [{value: _this.result.standard, name: '标准差'}]
                    }
                ]
            };

            var chart3 = echarts.init(document.getElementById('chart3'));
            chart3.setOption(option3);

            //callback
            callback && typeof callback == 'function' && callback();
        }
    }
});